﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HYSpace
{
	public partial class FormEstimateLoad : System.Windows.Forms.Form
	{
		public FormEstimateLoad()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.userSetting = new HVACUserSetting();
		}

		private void FormEstimateLoad_Load(object sender, EventArgs e)
		{
			this.dicEstimate = this.userSetting.GetHeatCoolLoadEstimate();
			this.InitialInterFaceInfo();
		}

		public bool BeSelectSpace()
		{
			return this.rdoSpace.Checked;
		}

		public double GetHeatLoad()
		{
			double result;
			try
			{
				result = Convert.ToDouble(this.hyTxtHeat.Text);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = 0.0;
			}
			return result;
		}

		public double GetCoolLoad()
		{
			double result;
			try
			{
				result = Convert.ToDouble(this.hyTxtCool.Text);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = 0.0;
			}
			return result;
		}

		private void InitialInterFaceInfo()
		{
			if (this.rdoSpace.Text == this.dicEstimate["Type"])
			{
				this.rdoSpace.Checked = true;
				this.rdoCustom.Checked = false;
				this.InitialHeatAndCool();
				this.hyTxtHeat.Enabled = false;
				this.hyTxtCool.Enabled = false;
				return;
			}
			this.rdoSpace.Checked = false;
			this.rdoCustom.Checked = true;
			this.InitialHeatAndCool();
			this.hyTxtHeat.Enabled = true;
			this.hyTxtCool.Enabled = true;
		}

		private void InitialHeatAndCool()
		{
			if (this.dicEstimate["Cool"].IsEmpty<char>())
			{
				this.hyTxtCool.Text = "90";
			}
			else
			{
				this.hyTxtCool.Text = this.dicEstimate["Cool"];
			}
			if (this.dicEstimate["Heat"].IsEmpty<char>())
			{
				this.hyTxtHeat.Text = "65";
				return;
			}
			this.hyTxtHeat.Text = this.dicEstimate["Heat"];
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.rdoCustom.Checked)
				{
					if (Convert.ToDouble(this.hyTxtHeat.Text) <= 0.0)
					{
						YJKMessageBox.Information("热负荷指标不能小于0！");
						return;
					}
					if (Convert.ToDouble(this.hyTxtCool.Text) <= 0.0)
					{
						YJKMessageBox.Information("冷负荷指标不能小于0！");
						return;
					}
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				YJKMessageBox.Information("负荷指标不能为空！");
				return;
			}
			this.SaveInterfaceInfo();
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void SaveInterfaceInfo()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			string value = this.rdoSpace.Checked ? this.rdoSpace.Text : this.rdoCustom.Text;
			dictionary.Add("Type", value);
			dictionary.Add("Heat", this.hyTxtHeat.Text);
			dictionary.Add("Cool", this.hyTxtCool.Text);
			this.userSetting.WriteHeatCoolLoadEstimate(dictionary);
		}

		private void rdoSpace_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoSpace.Checked)
			{
				this.hyTxtHeat.Enabled = false;
				this.hyTxtCool.Enabled = false;
				return;
			}
			this.hyTxtHeat.Enabled = true;
			this.hyTxtCool.Enabled = true;
		}

		private HVACUserSetting userSetting;

		private Dictionary<string, string> dicEstimate;
	}
}
